<template>
    <div>
    <p style="padding:10px; border-bottom:1px solid #f2f2f2; font-size:18px; margin-bottom:20px">商品添加</p>
    <el-form ref="form"  label-width="80px">
        <el-form-item label="商品名称" >
            <el-input style="width:300px;" v-model="goodsName" placeholder="商品名称"></el-input>
        </el-form-item>

        <el-form-item label="商品分类">
            <el-select  placeholder="请选择商品分类" :value='category' @change="checkChange">
            <el-option :label="item.cateName" :value="item.cateName" v-for="(item,index) in categories" :key="index"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="商品价格">
             <el-input-number v-model="price" @change="handleChange" :min="0" :max="9999" label="描述文字"></el-input-number>
        </el-form-item>

        <el-form-item label="商品图片">
         <el-upload  class="avatar-uploader"  action="http://127.0.0.1:5000/goods/goods_img_upload" :show-file-list="false" :on-success="handleAvatarSuccess"   >
        <img   v-if="uploadImg" :src="uploadImg" class="avatar">
        <i style="border:1px dashed #d9d9d9" v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>

        <el-form-item label="商品描述">
            <el-input style="width:300px" type="textarea" v-model="goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="success" @click="onSubmit">立即创建</el-button>
        </el-form-item>
    </el-form>
    </div>
</template>

<script>
import {categories,goodsadd} from '@/apis/goods'
   export default {
    data() {
      return {
        imageUrl: '',//商品图片
        uploadImg:'',//回填图片
        categories : [],//商品分类选项
        category:'',//商品分类
        goodsName:'',//商品名
        price:'',//商品价格
        goodsDesc:''//商品描述
      }
    },
    methods: {
       //商品价格
      handleChange(price) {
        // console.log(price);
        this.price = price
      },
        //商品图片
        handleAvatarSuccess(res,file) {
        if(res.code==0){
          this.uploadImg = URL.createObjectURL(file.raw);
          this.imageUrl =  res.imgUrl
          // console.log(this.imageUrl);
        }
      },
      //商品分类
      checkChange(cate){
        this.category = cate
      },
      // 点击立即创建
      onSubmit() {
      //添加商品
      goodsadd({
        name: this.goodsName,
        category: this.category,
        price: this.price,
        imgUrl: this.imageUrl,
        goodsDesc: this.goodsDesc,
      }).then(res=>{
        // console.log(res.data);
        if(res.data.code==0){
            this.$message({
              message: '系统提示：添加商品成功 ヾ(^▽^*)))',
              type: 'success'
            });
            this.uploadImg = ''
            this.category = ''
            this.goodsName = ''
            this.price = ''
            this.goodsDesc = ''
        }
      })
      },
    },
    created(){
      // 商品分类
        categories({}).then(res=>{
        this.categories = res.data.categories
        // console.log(this.categories);
      })
     this.$bus.emit('subTitle','商品管理>','商品添加>')

    }
  }
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>